/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package actions;

import Connexion.ConnexionCustomer;
import actions.facade.FacadeAction;
import com.opensymphony.xwork2.ActionContext;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.FinderException;
import miage.beans.ClientPresentation;
import miage.sessions.ecom.remote.EcomCustomerRemote;

/**
 *
 * @author TnP
 */
public class AuthentificationAction extends FacadeAction {
    
     private static EcomCustomerRemote rechercher;
     private String login;
     private String password;
     private ClientPresentation clientPresentation;
     private Map session = ActionContext.getContext().getSession();
     private int nbProduit;
     
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public int getNbProduit() {
        return nbProduit;
    }

    public void setNbProduit(int nbProduit) {
        this.nbProduit = nbProduit;
    }
    
    public void getNombreProduitDansPanier(){
        try{
            this.nbProduit = rechercher.nombreProduitInCart();
        }catch(Exception e)
        {
            this.nbProduit = 0;
            System.err.println(e);
        }        
    }    
    
    public String getControleAuthentification(){
        rechercher = ConnexionCustomer.connectionServerBean();
        getNombreProduitDansPanier();
        return "success";
    }
    
    public String getAuthentification(){
        
        rechercher = ConnexionCustomer.connectionServerBean();
        getNombreProduitDansPanier();
        try {
            //try{
            clientPresentation=rechercher.getAuthentification(login, password);
        } catch (FinderException ex) {
            Logger.getLogger(AuthentificationAction.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(AuthentificationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (clientPresentation != null) {

            session = ActionContext.getContext().getSession();
            
            session.put("connect", true);
            session.put("login", clientPresentation.getLogin());
            session.put("password", clientPresentation.getPassword());
            session.put("id", clientPresentation.getIban_client());
            session.put("statut", clientPresentation.getFlag_client_admin());      
            
            return "success";

        } else {
            return "input";
        }
        //}
        /*catch (CreateException ex) {
                Logger.getLogger(AuthentificationAction.class.getName()).log(Level.SEVERE, null, ex);
            }
         catch (FinderException ex) {
            Logger.getLogger(AuthentificationAction.class.getName()).log(Level.SEVERE, null, ex);
            return "input";
        } catch (RemoteException ex) {
            Logger.getLogger(AuthentificationAction.class.getName()).log(Level.SEVERE, null, ex);
            return "input";
        }*/
        
     } 
     public String deleteSession() {

        session = ActionContext.getContext().getSession();
        session.clear();
        return "success";

    }
}
